﻿using System;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
using Com.UmbracoChina.Consult.Models;
using Umbraco.Web;

namespace Com.UmbracoChina.Consult.Controllers
{
    /// <summary>
    /// 在线咨询前端控制器
    /// </summary>
    [PluginController("MyOnlineConsult")]
    public class ConsultFrontController : UmbracoApiController
    {
        void Test()
        {
            
            /***
             var ctx = UmbracoContext.Application.DatabaseContext;
             var helper = new DatabaseSchemaHelper(ctx.Database, UmbracoContext.Application.ProfilingLogger.Logger, ctx.SqlSyntax);
             */
        }

        private bool CheckConsultType(int id)
        {
            var content = new UmbracoHelper(UmbracoContext).TypedContent(id);
            if (content != null)
            {
                return content.DocumentTypeAlias.Equals("consultingList");
            }
            return false;
        }

        /// <summary>
        /// 新增问题
        /// </summary>
        /// <returns></returns>
        public ConsultResult Add()
        {
            var hc = UmbracoContext.HttpContext;
            ConsultResult result = new ConsultResult() { errCode = 0, errMessage = "初始化未激活" };


            string title = hc.Request.Form["title"].ToString();
            int type = int.Parse(hc.Request.Form["type"].ToString());
            string email = hc.Request.Form["email"].ToString();
            string content = hc.Request.Form["content"].ToString();

            int limit = 1;
            if (string.IsNullOrEmpty(title))
            {
                result.errCode = 101;
                result.errMessage = "请输入标题";
            }
            else if (type == 0)
            {
                result.errCode = 102;
                result.errMessage = "请选择咨询类别";
            }
            else if (!this.CheckConsultType(type))
            {
                result.errCode = 102;
                result.errMessage = "请选择正确的咨询类别";
            }
            else if (string.IsNullOrEmpty(email))
            {
                result.errCode = 103;
                result.errMessage = "请输入电子邮件";
            }
            else if (string.IsNullOrEmpty(content))
            {
                result.errCode = 104;
                result.errMessage = "请输入内容";
            }
            else
            {

                string ip = hc.Request.UserHostAddress;
                PetaPocoHelper<OnlineConsult> helper = new PetaPocoHelper<OnlineConsult>();
                OnlineConsult obj = PetaPocoHelper<OnlineConsult>.GetByStatement(string.Format("SELECT ID, Title, ConsultType, Email, ConsultContent, IP, IsSolve, AddTIme FROM OnlineConsolt WHERE IP = '{0}' AND AddTime > '{1}' ", ip, DateTime.Now.AddMinutes(0 - limit)));
                if (obj != null && obj.ID > 0)
                {
                    result.errCode = 201;
                    result.errMessage = limit + "分钟内内只能提交一次";
                }
                else
                {
                    //要新增
                    obj = new OnlineConsult();
                    obj.Title = hc.Server.UrlDecode(title);
                    obj.Email = hc.Server.UrlDecode(email);
                    obj.ConsultType = type;
                    obj.ConsultContent = hc.Server.UrlDecode(content);
                    obj.IP = ip;
                    obj.IsSolve = "n";
                    obj.AddTime = DateTime.Now;
                    var db = UmbracoContext.Application.DatabaseContext.Database;

                    try
                    {
                        db.Insert(obj);
                        result.errCode = 0;
                        result.errMessage = limit + "咨询已经提交，等待管理员回复！";
                    }
                    catch (Exception e)
                    {
                        UmbracoContext.Application.ProfilingLogger.Logger.Error(this.GetType(), e.Message, e);
                        result.errCode = -1;
                        result.errMessage = limit + "出现异常，请联系管理员！";
                    }
                }
            }
            return result;
        }
    }
}
